<?php

session_start(); //we need to start session in order to access it through CI

Class Login extends CI_Controller {

public function __construct() {
parent::__construct();

// Load form validation library
$this->load->library('form_validation');


// Load database
$this->load->model('login_database');

}

// Show login page
public function index() {
$this->load->view('layouts/header_view');	
$this->load->view('login_form');
}

// Show registration page
public function register() {
$this->load->view('layouts/header_view');	
$this->load->view('registration_form');
}

// Validate and store registration data in database
public function new_user_registration() {

// Check validation for user input in SignUp form
$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('email_value', 'Email', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
if ($this->form_validation->run() == FALSE) {
	$this->load->view('registration_form');	
	} 

else {
	$data = array(
		'name' => $this->input->post('name'),
		'username' => $this->input->post('username'),
		'email' => $this->input->post('email_value'),
		'password' => $this->input->post('password')
	);

$result = $this->login_database->registration_insert($data) ;

if ($result == TRUE) {
	//$data['message_display'] = 'Registracija je uspjesna!';
	//$this->load->view('login_form', $data);
	$this->session->set_flashdata('message', 'success');
	redirect('login');
	} 
else {
	$data['message_display'] = 'Korisnicko ime vec postoji!';
	$this->load->view('registration_form', $data);
	}
}
}

// Check for user login process
public function user_login_process() {

$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');

if ($this->form_validation->run() == FALSE) {
	$this->load->view('login_form');
} 
else {
	$data = array(
		'username' => $this->input->post('username'),
		'password' => $this->input->post('password')
	);
	$result = $this->login_database->login($data);
	if($result == TRUE){
		$sess_array = array(
			'username' => $this->input->post('username')
			);

		// Add user data in session
		$this->session->set_userdata('logged_in', $sess_array);
		$result = $this->login_database->read_user_information($sess_array);
		if($result != false){
			$data = array(
				'name' =>$result[0]->name,
				'username' =>$result[0]->username,
				'email' =>$result[0]->email,
				'password' =>$result[0]->password
				);
			//$this->load->view('admin_page', $data);
			redirect('site',$data);
			
		}
	}
	else{
		$this->session->set_flashdata('message', 'error');
		redirect('login');
		}
	}
}


// Logout from admin page
public function logout() {

// Removing session data
	$sess_array = array(
		'username' => ''
		);
	$this->session->unset_userdata('logged_in', $sess_array);

	$this->session->set_flashdata('message', 'logout');
	redirect('login');
	}
}

?>